package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
)

// ListPromotionPtrsAsOfferOfItemIDParam 根据商品列出作为优惠项目的促销活动参数
type ListPromotionPtrsAsOfferOfItemIDParam struct {
	OwnerID int64
	Issuer  string
	ItemID  int64
}

// ListPromotionPtrsAsOfferOfOwner 根据所有者列出作为优惠项目的促销活动
func (s *PromotionEntityServiceImp) ListPromotionPtrsAsOfferOfOwner(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListPromotionPtrsAsOfferOfItemIDParam) (promotionPtrs []*sqlc.Promotion, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	// slog.Infof("cPtr=>%+v", *cPtr)
	// slog.Infof("ptr=>%+v", *ptr)

	promotions, err := q.ListAvailablePromotionsByOwnerID(ctx, sqlc.ListAvailablePromotionsByOwnerIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		Ts:      cPtr.PagerPtr.Ts,
		Offset:  cPtr.PagerPtr.Offset,
		Limit:   cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	promotionPtrs = s.MapPromotionEntityToPtr(promotions)
	return
}

// ListPromotionPtrsAsOfferOfItemID 根据商品列出作为优惠项目的促销活动
func (s *PromotionEntityServiceImp) ListPromotionPtrsAsOfferOfItemID(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListPromotionPtrsAsOfferOfItemIDParam) (promotionPtrs []*sqlc.Promotion, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("cPtr=>%+v", *cPtr)
	slog.Infof("ptr=>%+v", *ptr)
	rowsMatchIncludeItemID, err := q.ListAvailablePromotionsMatchIncludeItemID(ctx, sqlc.ListAvailablePromotionsMatchIncludeItemIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		ItemID:  ptr.ItemID,
	})
	if err != nil {
		return
	}
	promotionPtrs = append(promotionPtrs, s.MapPromotionEntityToPtr(rowsMatchIncludeItemID)...)
	rowsMatchExcludeItemID, err := q.ListAvailablePromotionsMatchExcludeItemID(ctx, sqlc.ListAvailablePromotionsMatchExcludeItemIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		ItemID:  ptr.ItemID,
	})
	if err != nil {
		return
	}
	promotionPtrs = append(promotionPtrs, s.MapPromotionEntityToPtr(rowsMatchExcludeItemID)...)
	return
}

// ListAvailablePromotionsForDoTrialParam 列出可用的试算促销活动参数
type ListAvailablePromotionsForDoTrialParam struct {
	BrandID  int64
	OwnerID  int64
	Issuer   string
	UseScene string
}

// ListAvailablePromotionsForDoTrial 列出可用的试算促销活动
func (s *PromotionEntityServiceImp) ListAvailablePromotionsForDoTrial(ctx context.Context, q *sqlc.Queries, ptr *ListAvailablePromotionsForDoTrialParam) (promotionPtrs []*sqlc.Promotion, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	promotions, err := q.ListAvailablePromotionsByBrandIDOwnerIDIssuerAndUseScene(ctx, sqlc.ListAvailablePromotionsByBrandIDOwnerIDIssuerAndUseSceneParams{
		BrandID:  ptr.BrandID,
		OwnerID:  ptr.OwnerID,
		Issuer:   ptr.Issuer,
		UseScene: ptr.UseScene,
	})
	if err != nil {
		return
	}
	promotionPtrs = s.MapPromotionEntityToPtr(promotions)
	return
}
